/**
 * 定义一个Get 方法装饰器
 */
interface IGetList {
  (list: { list: Array<any> }): void
}

const mockApi = (url: string) => {
  console.log('请求url地址参数', url)

  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        list: [
          {
            id: 1,
            name: '张三'
          },
          {
            id: 1,
            name: '张三'
          }
        ]
      })
    }, 2000)
  })
}

function Get(url: string): MethodDecorator {
  return (target, key, descriptor) => {
    // 拿到getList原始方法
    const originMethods = descriptor.value as IGetList

    // 先进行axios请求， 再将请求的结果，传给 originMethods

    mockApi(url)
      .then((res) => {
        originMethods(res as { list: Array<any> })
      })
      .catch((err) => {
        // 如果发生错误，也可以将错误信息传递给下 getList
        originMethods(err)
      })
  }
}

const Post = (url: string): MethodDecorator => {
  return (target, key, descriptor) => {
    console.log(key, url, descriptor)
  }
}

class Constroller {
  @Get('/api/getList')
  getLit(res: any) {
    console.log(res)
  }

  @Post('xxx')
  create() {
    console.log('Post  装饰器')
  }
}

new Constroller()

// 启动程序  ts-node 05-decorator-fetch-example.ts
